<?php
/*
File Name:		functions.php
Description: 	Contiene le funzioni comuni utilizzate nel plugin
Plugin URI:  	http://www.SyriusWeb.com/
Author:      	SyriusWeb
Author URI:  	http://www.SyriusWeb.com/
*/

function fascicoli_set_lang_dir() {
	$locale = apply_filters('plugin_locale', get_locale(), FASCICOLI_DOMAIN);	
	load_textdomain(FASCICOLI_DOMAIN, WP_LANG_DIR.'/'.FASCICOLI_DOMAIN.'/'.FASCICOLI_DOMAIN.'-'.$locale.'.mo');
	load_plugin_textdomain(FASCICOLI_DOMAIN, FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
}


// verifica se ci sono azioni da fare 
function fascicoli_dotask($the_content) {
	global $post;
	
	if ( ($post->post_type != 'fascicolo') || (empty($_REQUEST['task'])) || (empty($_REQUEST['type'])) ) {
		return;
	}
	
	if (current_user_can('edit_fascicoli')) {
		// @TODO request va un po' elaborato
		$args = $_REQUEST;
		do_action('task_'.$_REQUEST['type'].'_'.$_REQUEST['task'], $args);
	 	return $the_content;
	} else {
		return;
	}	
}


//Toglie la creazione automatica dell'excerpt se è vuoto
function fascicoli_fascicolo_trim_excerpt($trimmed, $raw) {
    if ($raw == '') {
    	return $raw;
    } else {
    	return $trimmed;
    }
}


function fascicoli_no_permissions() {	
	$msg = "<p>" . __( 'Sorry, you have not permissions for this page', H8_DOMAIN ) . "</p>";	
	return $msg;
}


/*
// inserisce un termine di tassonomia, specificando anche custom field
function insert_term($term, $taxonomy, $args) {
       
       wp_insert_term($term,$taxonomy,$args);       
       unset($args['description'],$args['parent'],$args['slug']);
       $data = serialize($args);
       $name = 'field_'.$taxonomy.'_'.$term;
       if (get_option($name,'')!='') {
		update_option($name,$data);
       } else {
		add_option($name,$data,'',true);
       }
}
*/


add_action('admin_head', 'cpf_add_mceEditor');	
// aggiune un javascript per la pagina di creazione degli stati
function cpf_add_mceEditor() { 
?>
<script type="text/javascript" >
function cpf_add_mceEditor_javascript(id) {

	jQuery(document).ready(function() {
	jQuery("#"+id).addClass("mceEditor");
	if ( typeof( tinyMCE ) == "object" && typeof( tinyMCE.execCommand ) == "function" ) {
	tinyMCE.execCommand("mceAddControl", false, id);
	}

	});
	
}
</script>
<?php
}




function show_field($id, $type, $options, $default, $localization='', $message='', $return=false ) {
  // add a field in form for admin panel
  //   type is the field type :
  //      littlenumber
  //      text
  //      color
  //      page : a page-break in the admin panel
  //      longtext
  //	  checkbox
  //	  hidden
  //	  button
  //	  readonly
  //	  date
  //	  file
  //	  files
  //      formattedtext
	
	
    if (is_array($options)) {
        $text  = $options['description'];
        $text2 = $options['values'];
        $text3 = $options['conclusion'];
        $default2 = $options['default2'];
    } else {
        @list($text, $text2, $text3) = @explode('|', $options, 3);
    }
    
    $string = '<div class="field_wrapper form-field">';
    
    switch($type) {
    	
		case 'break':
		    $string.= '<p></p>';
		break;
	
        case 'color':
        	$string.= '
          <label for="'.$id.'" class="label">'.__($text,$localization).'</label>
          #<input type="text" id="'.$id.'" maxlength="6" name="'.$id.'" value="'.$default.'"
	      size="6" onchange="ChangeColor(\''.$id.'_box\',this.value)" onkeyup="ChangeColor(\''.$id.'_box\',this.value)"/>
	      <span id="'.$id.'_box" style="background:#'.$default.'">&nbsp;&nbsp;&nbsp;</span>';
	      if ($message) {
	      	$string .= '<p>'.__($message,$localization).'</p>';
		  }
	      
        break;
        
		case 'date':
		       $default= date_create_from_format('Y-m-d',$default);
		       if(!$default) $default=date_create();
			$default= $default->format('d-m-Y');
			$string.= '
			<label for="'.$id.'" class="label">'.__($text,$localization).'</label>
			<input type="text" name="'.$id.'" id="'.$id.'" value="'.$default.'" class="Datepicker" style="width:100px;"/>';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
		break;
		
		case 'file':
		    $string.= '<label for="'.$id.'">'.__($text,$localization).'</label>';
	            if ($default) $string.="&nbsp;<a href='$default'>$default</a> <input type='checkbox' name='{$id}_delete' id='$id' />".__('Delete',$localization);
	            else $string.='<div class="file-input"><input id="'.$id.'" class="async-upload" type="file" name="'.$id.'" /></div>';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
		break;
		
		case 'files':
		    $string.= '<label for="'.$id.'[]">'.__($text,$localization).'</label>
			<div class="new-files">
			    <div class="file-input"><input id="'.$id.'[]" class="async-upload" type="file" name="'.$id.'[]" /></div>
			    <a class="admin-add-file" href="javascript:void(0)">' . _('Add more files') . '</a>
			</div>';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
		    $string.= '<script type="text/javascript">
			jQuery(document).ready(function($) {
				// add more file
				$(".admin-add-file").click(function(){
					var $first = $(this).parent().find(".file-input:first");
					$first.clone().insertAfter($first).show();
					return false;
				});
			});
			</script>';
		break;

        case 'page':
		$string.= '
             	</div>
            	</fieldset>
	            <br />
                <fieldset>
	            <legend><b>'.__($text,$localization).'</b></legend>
	            <div class="Option">';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;
        
        case 'select':
			$string.= '
			<label for="'.$id.'" class="label">'.__($text,$localization).'</label>
          	<select class="field_select" id="'.$id.'" name="'.$id.'" >';
          	$options = explode(",",$text2);
          	foreach($options as $opt) {
				$v=$opt;$k=$opt;
				
          			if (strpos($opt,':')) list($k,$v)=explode(':',$opt,2);
				if ($k=='') $k=$v;
				
				if ($default==$k)	$d = ' selected="selected" ';
          			else			$d = ' ';
				
          			$string.= "<option value='$k' $d >".__($v,$localization)."</option>";
          	}
          	$string.= '
          	</select>';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;
        
        case 'select&text':
			$string.= '
			<label for="'.$id.'" class="label">'.__($text,$localization).'</label>
          <select class="field_select" id="'.$id.'" name="'.$id.'" >';
          	$options = explode(",",$text2);
          	foreach($options as $opt) {
				$v=$opt;$k=$opt;
				
          			if (strpos($opt,':')) list($k,$v)=explode(':',$opt,2);
				if ($k=='') $k=$v;
				
				if ($default==$k)	$d = ' selected="selected" ';
          			else			$d = ' ';
				
          			$string.= "<option value='$k' $d >".__($v,$localization)."</option>";
          	}
          $string .= '
          </select>
          <input type="text" maxlength="300" name="'.$id.'_option" id="'.$id.'_option" value="'.$default2.'" style="width:150px;" />'.$text3.'<br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;   		
        
        case 'text':
          	$string.= '
          	<label class="label" for="'.$id.'">'.__($text,$localization).'</label>
          	<input type="text" maxlength="300" style="width:300px;" name="'.$id.'" id="'.$id.'" value="'.$default.'"  />'.__($text2,$localization).'<br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break; 
        
        case 'littlenumber':	
		case 'smallesttext':
		  $string.= '
	          <label class="label" for="'.$id.'">'.__($text,$localization).'</label>
	          <input type="text" maxlength="5" name="'.$id.'" value="'.$default.'" style="width:50px;" /> '.__($text2,$localization).'<br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
	        break;
	        
        case 'smalltext':
		  $string.= '<label class="label" for="'.$id.'">'.__($text,$localization).'</label>
	          <input type="text" maxlength="100" name="'.$id.'" id="'.$id.'" value="'.$default.'" style="width:100px;" /> '.__($text2,$localization).'<br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;
        
        case 'longtext':
			if ($text!='') 
				$string.= '<label for="'.$id.'" class="label">'.__($text,$localization).'</label><br />';
			$string.= '<textarea  name="'.$id.'" id="'.$id.'" cols="60" rows="5" style="width:99%">'.$default.'</textarea> '.__($text2,$localization).'<br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;
        
        case 'formattedtext':       	
        	
        	echo '<SCRIPT LANGUAGE="javascript">';
			echo "cpf_add_mceEditor_javascript('".$id."');";
			echo '</SCRIPT>';
        	
        	
			if ($text!='') 
				$string.= '<label for="'.$id.'" class="label">'.__($text,$localization).'</label><br />';
		
            $string.= '<textarea name="'.$id.'" id="'.$id.'" cols="60" rows="5" style="width:99%" class="additional-info form-input-tip code" size="20" autocomplete="off" >'.$default.'</textarea> '.__($text2,$localization).'<br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;
        
        case 'hidden':
          $string.= '<input type="hidden" name="'.$id.'" id="'.$id.'" value="'.$default.'" />';
        break;
        
        case 'button':
        	// @TODO button non funziona
           $string.= '<input type="button" name="'.$id.'" id="'.$id.'" value="'.$text.'" />';
        break;        
        
       case 'readonly':
		  $size = strlen($default);  
	          $string.= '
	          <label class="label" for="'.$id.'">'.__($text,$localization).'</label>
		  <input type="hidden" name="'.$id.'" id="'.$id.'" value="'.$default.'" />
		  <span style="color:darkgray; font-family: Consolas,Monaco,monospace; font-style: italic;border: solid 1px; background: none repeat scroll 0 0 #EAEAEA;">&nbsp;'.$default.'&nbsp;</span>'.__($text2,$localization).'<br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;
        
        case 'checkbox':
		$string.= '<label for="'.$id.'" class="label">'.__($text,$localization).'</label><input type="checkbox" name="'.$id.'" id="'.$id.'" ';
		if($default == '1') { $string.= ' checked="checked" '; }
		$string.= ' /><br />';
		      if ($message) {
		      	$string .= '<p>'.__($message,$localization).'</p>';
			  }
        break;
    }
    $string.= '</div>';
    
    if ($return) {    	
    	return $string;
    } else {    	
    	echo $string;
    	
    }   
}